//
//  CMSChartsViewHeaderView.m
//  CMSCharts
//
//  Created by symbio on 2023/2/14.
//

#import "CMSChartsViewHeaderView.h"
#import <CMSFoundation/CMSFoundation.h>
#import <Masonry/Masonry.h>
#import <CMSUIComponents/CMSSegmentHeaderView.h>
#import <CMSUIKit/CMSUIKit.h>

@interface CMSChartsViewHeaderView ()
@property (nonatomic, strong) CMSSegmentHeaderView *segmentHeaderView;

@property (nonatomic, strong) UIButton *settingButton;

@end

@implementation CMSChartsViewHeaderView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUISegment];
    }
    return self;
}

- (void)setViewModel:(CMSChartsViewModel *)viewModel {
    _viewModel = viewModel;
    [self.segmentHeaderView layoutIfNeeded];
    NSMutableArray *muarr = [NSMutableArray array];
    for (CMSChartsConfigModel *object in viewModel.visibleChartConfigs) {
        CMSSegmentButtonItem *item = [[CMSSegmentButtonItem alloc] init];
        item.title = object.name;
        [muarr addObject:item];
    }
    
    NSArray *invisibles = viewModel.invisibleChartConfigs;
    if (invisibles.count > 0) {
        CMSSegmentButtonItem *item = [[CMSSegmentButtonItem alloc] init];
        if (![viewModel.visibleChartConfigs containsObject:viewModel.currentConfigModel]) {
            item.title = viewModel.currentConfigModel.name;
        } else {
            item.title = @"周期";
        }
        [muarr addObject:item];
    }

    self.segmentHeaderView.items = muarr;
    [self.segmentHeaderView setSelectedIndex:viewModel.currentIndex animated:NO];
    
}

- (void)setupUISegment {
    [self addSubview:self.settingButton];
    [self.settingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(self);
        make.width.mas_equalTo(40);
    }];
    
    [self addSubview:self.segmentHeaderView];
    [self.segmentHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self);
        make.right.equalTo(self.settingButton.mas_left);
    }];
}

- (CMSSegmentHeaderView *)segmentHeaderView {
    if (!_segmentHeaderView) {
        _segmentHeaderView = [[CMSSegmentHeaderView alloc] init];
        _segmentHeaderView.needLine = YES;
        _segmentHeaderView.margin = 0;
//        _segmentHeaderView.itemSpacing = 24;
        _segmentHeaderView.bounces = false;
        _segmentHeaderView.screenFitButton = YES;
        @weakify(self);
        _segmentHeaderView.didSelectItemBlock = ^(CMSSegmentHeaderView *headerView, NSInteger index) {
            @strongify(self)
            [self handleSegmentViewWithIndex:index];
        };
    }
    return _segmentHeaderView;
}

- (void)handleSegmentViewWithIndex:(NSInteger)index {
    NSArray *visibles = self.viewModel.visibleChartConfigs;
    if (visibles.count > index) {
        if (self.didSelectItemBlock) {
            self.didSelectItemBlock(visibles[index], index);
        }
        return;
    }
        
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"周期" message:nil preferredStyle:(UIAlertControllerStyleAlert)];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        [self.segmentHeaderView setSelectedIndex:self.viewModel.currentIndex animated:YES];
    }]];
    
    for (CMSChartsConfigModel *model in self.viewModel.invisibleChartConfigs) {
        [alert addAction:[UIAlertAction actionWithTitle:model.name style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            if (self.didSelectItemBlock) {
                self.didSelectItemBlock(model, index);
            }
        }]];
    }
    [self.cms_viewController presentViewController:alert animated:YES completion:nil];
}

- (UIButton *)settingButton {
    if (!_settingButton) {
        _settingButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_settingButton setTitle:@"设置" forState:(UIControlStateNormal)];
        @weakify(self);
        [_settingButton cms_addEventHandler:^(id  _Nonnull sender) {
            @strongify(self)
            UIViewController *viewController = [UIViewController new];
            viewController.view.backgroundColor = [UIColor whiteColor];
            [self.cms_viewController.navigationController pushViewController:viewController animated:YES];
        } forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _settingButton;
}

@end
